<?php
/**
 * API转账管理
 */
namespace app\api\model;
use app\common\model\MemberTransfer as MemberTransferModel;
use app\common\common;
use app\common\model\Member as MemberModel;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use think\facade\Db;

class MemberTransfer extends MemberTransferModel
{

    /**
     * 查询提现记录列表
     * @param array $where  查询条件
     * @param int $page 页码
     * @param int $limit    每页数量
     * @param string $order 排序
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function getList($where = [],$page = 1,$limit = 20,$order = 'id desc'){
        $data = self::where($where)->order($order)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();
        return $data;
    }

    /**
     * 添加转账
     * @param $mid  转账用户
     * @param $currency_num 转账币种
     * @param $money    转账金额
     * @param $address  接收方地址
     * @param $receive_mid  接收方用户ID
     * @param $charge_money 手续费
     * @param $remarks 备注
     * @return false|int|string
     */
    public static function add_transfer($mid,$currency_num,$money,$address,$receive_mid,$charge_money,$remarks = ''){
        $data = [
            'mid'   =>  $mid,
            'currency'  =>  $currency_num,
            'money' =>  $money,
            'address'   =>  $address,
            'receive_mid'   =>  $receive_mid,
            'charge_money'  =>  $charge_money,
            'remarks'       =>  $remarks,
            'add_time'  =>  date('Y-m-d H:i:s',time())
        ];
        $id = self::insertGetId($data);

        if($id){
            //币种
            $currency = common::key_currency($currency_num);
            $main_status = UserMoneyLogModel::change($mid,$currency,-$money,'transfer',$id,'转账：'.$money."到".$address);
            //扣款失败
            if($main_status != true) return false;
            //判断手续费
            if($charge_money>0){
                $charge_status = UserMoneyLogModel::change($mid,$currency,-$charge_money,'Miners fee',$id,'转账：'.$money."到".$address.',手续费:'.$charge_money);
                if($charge_status != true) return false;
            }
            //收款
            $receive_status = UserMoneyLogModel::change($receive_mid,$currency,$money,'transfer',$id,'好友转账：'.$money);
            if($receive_status != true) return false;

            return $id;
        }else{
            return false;
        }

    }
}